/**
 * Created by zhaojm on 15/3/28.
 */


game.Enemy = game.RoleProtocol.extend({

    _layer : null,
    _type : null,
    _delayTime : null,
    _delayTimeCount : null,
    _HP : null,

    ctor:function(type) {
        this._super();
        var winSize = cc.winSize;
        this._type = type;
        this._HP = type.HP;
        this._delayTime = 1 + 1.2 * Math.random();
        this._delayTimeCount = 0;

        this._sprite = new cc.Sprite(type.textureName);
        var pos = cc.p( 80 + (winSize.width - 160) * Math.random(), winSize.height);
        this._sprite.setPosition(pos);

    },

    destroy : function(){

        game.Config.addScoreValue(this._type.scoreValue );

        var explode = new game.ExplodeEffect();
        explode.addToLayer(this._layer, this._sprite.getPosition());

        var spark = new game.SparkEffect();
        spark.addToLayer(this._layer, this._sprite.getPosition(), 1.2, 0.7);

        this.removeFromLayer();

        // ����
        //if (Config::sharedConfig()->getAudioState()) {
        //    SimpleAudioEngine::sharedEngine()->playEffect(s_explodeEffect);
        //}
    },
    hurt : function(){
        this._HP--;
    },
    collideRect : function(){
        var size = this._sprite.getContentSize();
        var pos = this._sprite.getPosition();

        return cc.rect(pos.x - size.width / 2, pos.y - size.height / 4, size.width, size.height / 2);
    },

    update:function(dt){
        var winSize = cc.winSize;
        if (this._HP <= 0) {
            this.destroy();
            return;
        }
        var screenRect = cc.rect(0, 0,  winSize.width, winSize.height + 10);
        if((!cc.rectIntersectsRect(screenRect, this.collideRect()))){
            this.removeFromLayer();
        }

        this._delayTimeCount += dt;
        if(this._delayTimeCount >= this._delayTime){
            this._delayTimeCount = 0;
            this.shoot();
        }

    },

    shoot:function(dt) {
        var pos = this._sprite.getPosition();
        var bullet = game.Bullet.createEnemyBullet();
        bullet.addToLayer(this._layer);
        this._layer.enemy_bullet.push(bullet);
        bullet.setPosition(cc.p(pos.x, pos.y - this._sprite.getContentSize().height * 0.2));

    },

    addToLayer : function(layer){
        this._layer = layer;
        var winSize = cc.winSize;

        var contentSize = this._sprite.getContentSize();
        var pos = this._sprite.getPosition();

        var offset;
        var tempAction;
        var a0;
        var a1;
        var onComplete;

        switch (this._type.moveType) {
            case 0:
                if (layer.getShip()) {
                    offset = layer.getShip().getPosition();
                }else{
                    offset = cc.p(winSize.width / 2, 0);
                }
                tempAction = cc.MoveTo.create(1, offset);

                break;
            case 1:
                offset = cc.p(0, -winSize.height - contentSize.height);
                tempAction = cc.MoveBy.create(4, offset);
                break;

            case 2:
                offset = cc.p(0, -100 - 200 * Math.random());
                a0 = cc.MoveBy.create(0.5, offset);
                a1 = cc.MoveBy.create(1, cc.p(-50 - 100 * Math.random(), 0));

                onComplete =  cc.CallFunc.create(this.repeatAction);
                tempAction = cc.Sequence.create(a0, a1, onComplete);
                break;

            case 3:
                var newX = (pos.x <= winSize.width / 2 ) ? 320 : - 320;
                a0 = cc.MoveBy.create(4, cc.p(newX, -240));
                a1 = cc.MoveBy.create(4, cc.p(-newX, -320));
                tempAction = cc.Sequence.create(a0, a1);
                break;

        }

        layer.addChild(this._sprite);
        layer.enemy_items.push(this);
        this._sprite.runAction(tempAction);

    },

    removeFromLayer : function(){
        this._layer.enemy_items.remove(this);
        this._layer.enemy_items.remove(this);
        this._sprite.removeFromParent();
    },


    repeatAction:function(pSender){
        var delay =  cc.DelayTime.create(1);
        var mv = cc.MoveBy.create(1, cc.p(100 + 100 * Math.random(), 0));
        var seq = cc.Sequence.create(delay, mv, delay.clone(), mv.reverse());
        pSender.runAction(cc.RepeatForever.create(seq));
    },


});